var ID=getQueryVariable("yp")
window.onload=function () {
    $.ajax({
        type:"GET",
        url:"/category/getCategories",

        success:function (data) {
            //console.log(data[0].name)
            var chang=data.length
            var wyp=""
            for(var i=0;i<chang;i++){
                wyp+="<option id='"+data[i].id+"' value='"+data[i].id+"' selected='false'>"
                wyp+=data[i].name
                wyp+="</option>"
            }
            $("#hide").after(wyp)
        },
        error:function () {
            console.log("error")
        }
    })

    $.ajax({
        type:"get",
        url:"/goods/getGoodsById",
        data:{
            id:ID
        },
        success:function (data) {
            $("#name").val(data.goods.name);
            $("#price").val(data.goods.price);
            $("#store").val(data.goods.store);
            document.getElementById("typppp").value = data.category.id;
            document.getElementById("typppp")[data.category.id].selected=true;
        }
    })
}

function Change() {
    var name = document.getElementById("name").value;
    var price = document.getElementById("price").value;
    var store = document.getElementById("store").value;
    var cId=$("#typppp").find("option:checked").attr("id");
    var cName="";
    var date={
        "category": {
            "id": cId,
            "name": cName,
        },
        "goods": {
            "id": ID,
            "name": name,
            "price": price,
            "store": store
        }
    };
    $.ajax({
        type:"POST",
        url:"/goods/saveOrUpdateGoods",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(date),
        success:function () {
            alert("添加成功！")
            window.location.href="messageguanli.html"
        }
    })
}

function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}